package com.shanshan.system.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2022/09/26 14:48
 * @Description 上传到本地
 */
public class UploadUtil {
    public static String uploadLocal(MultipartFile file, HttpServletRequest request) {

        //获取上传路径
        String path = request.getSession().getServletContext().getRealPath("upload");

        File target = new File(path);
        if (!target.exists()) {
            //创建文件夹
            target.mkdirs();
        }
        //把上面的文件转移到target目录下。 IO
        System.out.println(file.getOriginalFilename());

        String filename = UUID.randomUUID() + "-" + file.getOriginalFilename();
        File fileNew = new File(path + "/" + filename);
        try {
            file.transferTo(fileNew);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path + "/" + filename;
    }

    public static String uploadOOS(MultipartFile file) {
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";

//        https://springmvc0327.oss-cn-hangzhou.aliyuncs.com/cf129a97-cecb-4a9d-aeab-078b7a59c482-aaa.jpg

        String accessKeyId = "LTAI5tMcTPWGyQgffLN1rdKZ";
        String accessKeySecret = "IKCHysWEWs4vRkvfCIkp12BcgQZqKp";

        String bucketName = "springmvc0327";


        String objectName = UUID.randomUUID() + "-" + file.getOriginalFilename();

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            //获取file中的输入流
            InputStream inputStream = file.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName, inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return "https://" + bucketName + "." + endpoint + "/" + objectName;
    }
}
